﻿/******************************************************************************************************************************
 * Main form for RecipeBox application that displays a list of stored recipes and allows the filtering based off of key word(s)
 * Author: Morgan Davis
 * Northwestern Michigan College (student assignment)
 * Created: 27 November 2013
 * ****************************************************************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace RecipeBox
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
            
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            RecipeXML.LoadXML(); //call method from RecipeXML class to instantiate XDocument object
            FillListBox(); //populate list box with recipe objects
        }

        //method calls GetAllRecipes method from RecipeXML class, then places the objects into the list box
        private void FillListBox()
        {
            lstRecipes.Items.Clear();
            List<Recipe> forList = RecipeXML.GetAllRecipes();
            foreach (var x in forList)
                lstRecipes.Items.Add(x);
        }

        //when user double clicks a recipe in the list box it opens the recipe to viewed in detail in a seperate form
        private void lstRecipes_DoubleClick(object sender, EventArgs e)
        {
            if (lstRecipes.SelectedIndex >= 0)
            {
                Form viewRecipe = new frmViewRecipe(lstRecipes.SelectedItem as Recipe);
                viewRecipe.ShowDialog();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit(); //exit the application
        }

        //when user clicks add button, another form is opened to allow the input of recipe info, listbox is repopulated 
        //after recipe creation
        private void btnAdd_Click(object sender, EventArgs e)
        {
            using (var Form = new frmAddRecipe())
            {
                var result = Form.ShowDialog();
                if (result == DialogResult.OK)
                {
                    FillListBox();
                }
            }
        }

        //sends a recipe object to the RemoveRecipe method in the RecipeXML class to be deleted after user confirmation
        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (lstRecipes.SelectedIndex >= 0) //make sure the user has selected a recipe first
            {
                DialogResult myResult = MessageBox.Show("Are you sure you want to permanently delete this Recipe?", "Verify Recipe Deletion", MessageBoxButtons.YesNo);
                if (myResult == DialogResult.Yes)
                {
                    Recipe recipeToDelete = lstRecipes.SelectedItem as Recipe;
                    RecipeXML.RemoveRecipe(recipeToDelete);
                    FillListBox();
                }
                else
                {
                    MessageBox.Show("Recipe Deletion Cancelled");
                }
            }
            else 
            {
                MessageBox.Show("Please select a recipe from the list to be deleted.", "Select a Recipe First!");
            }
        }

        //uses the GetAllRecipes method from the RecipeXML class, then filters the results to be displayed in the list box
        //based on the key word/words the user entered into the filter text box
        private void btnFilter_Click(object sender, EventArgs e)
        {
            List<Recipe> filteredList = RecipeXML.GetAllRecipes();
            var filter = from x in filteredList.Where(x => x.name.ToUpper().Contains(txtFilter.Text.ToUpper())) select x;
            lstRecipes.Items.Clear();
            foreach (Recipe x in filter)
            {
                lstRecipes.Items.Add(x);
            }
        }
    }
}
